package com.sxran.im.controller;

import cn.hutool.core.util.RandomUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.sxran.im.dao.ImUserMapper;
import com.sxran.im.info.ImFriend;
import com.sxran.im.info.ImUser;
import com.sxran.im.service.JWTService;
import io.openvidu.java.client.*;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import javax.annotation.PostConstruct;
import javax.annotation.Resource;
import java.util.Map;

/**
 * 用户相关的接口：
 * 注册,登陆。
 */
@CrossOrigin(origins = "*")
@RestController
public class UserController {
	@Resource
	private ImUserMapper imUserMapper;
	@Resource
	private JWTService jwtService;




	/**
	 * @return The Session ID
	 */
	@PostMapping("/api/user/register")
	public CommonResult<Integer> register(@RequestBody ImUser user){
		user.setAvatar("http://images.rednet.cn/articleimage/2013/01/23/1403536948.jpg");
		user.setStatus(2);
		user.setSign("");
		user.setTerminal("");
		int insert = imUserMapper.insert(user);
		return CommonResult.success(insert);
	}
	@PostMapping("/api/user/login")
	public CommonResult<ImUser> login(@RequestBody ImUser user){
		LambdaQueryWrapper<ImUser> queryWrapper = new LambdaQueryWrapper<>();
		String userId = user.getUserId();
		queryWrapper.eq(ImUser::getUserId, userId);
		queryWrapper.eq(ImUser::getPassword,user.getPassword());
		ImUser data = imUserMapper.selectOne(queryWrapper);
		if(data ==null){
			return CommonResult.error(10008,"账号/密码不对");
		}else{
			// 模拟生成token内容。
			String token = jwtService.createToken(userId);
			data.setToken(token);
			return CommonResult.success(data);
		}

	}
}
